package com.cqgc.controller;

import com.cqgc.domain.*;
import com.cqgc.service.BarnService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("barn")
public class BarnController {
    @Autowired
    private BarnService barnService;

    bResult result = new bResult();

    @PostMapping("list")
    public bResult findAll(@RequestBody PageHelper pageHelper) {
//        Barn barn = barnPage.getBarn();
//        PageHelper pageHelper = barnPage.getPageHelper();
        if (pageHelper != null) {
            int page = (pageHelper.getCurrentPage() - 1) * pageHelper.getPageSize();
            pageHelper.setCurrentPage(page);
        }
        List<Barn> all = barnService.findAll(pageHelper);
        List<Barn> count = barnService.findAllBarn();
        if (all != null) {
            result.setCode(1);
            result.setMsg("查询成功");
            result.setData(all);
            System.out.println(result.getData());
            result.setTotal(count.size());
        } else {
            result.setCode(0);
            result.setMsg("查询失败");
        }
        return result;
    }
    @GetMapping("look")
    public bResult Search(int bid) {
        Barn BarnByBid = barnService.findBarnByBid(bid);
        if (BarnByBid != null) {
            result.setCode(1);
            result.setMsg("查找成功");
        } else {
            result.setCode(0);
            result.setMsg("查找失败");
        }
        return result;
    }

    @PostMapping("edit")
    public bResult Edit(@RequestBody Barn barn){
        Barn BarnByBid = barnService.findBarnByBid(barn.getBid());
        if (barn != null) {
            if (barn.getBname() == null) {
                barn.setBname(BarnByBid.getBname());
            }
            if (barn.getLan() == null) {
                barn.setLan(BarnByBid.getLan());
            }
            if (barn.getEr() == null) {
                barn.setEr(BarnByBid.getEr());
            }
            if (barn.getYvjing() == null) {
                barn.setYvjing(BarnByBid.getYvjing());
            }
        } else {
            result.setCode(0);
            result.setMsg("修改失败");
            return result;
        }
        int i = barnService.editBarn(barn);
        if (i > 0) {
            result.setCode(1);
            result.setMsg("修改成功");
        } else {
            result.setCode(0);
            result.setMsg("修改失败");
        }
        return result;
    }

    @PostMapping("add")
    public bResult AddUser(@RequestBody Barn barn) {
        int i = barnService.addBarn(barn);
        if (i > 0) {
            result.setCode(1);
            result.setMsg("注册成功");

        } else {
            result.setCode(0);
            result.setMsg("注册失败");
        }
        return result;
    }
    @PostMapping("del")
    public bResult delHarvest(@RequestBody Barn barn) {
        int i = barnService.deleteBarn(barn);
        if (i > 0) {
            result.setCode(1);
            result.setMsg("删除成功");
        } else {
            result.setCode(0);
            result.setMsg("删除失败");
        }

        return result;
    }

    @PostMapping("ban")
    public bResult banHarvest(@RequestBody Barn barn) {
        int i = barnService.banBarn(barn);
        if (i > 0) {
            result.setCode(1);
            result.setMsg("禁用成功");
        } else {
            result.setCode(0);
            result.setMsg("禁用失败");
        }

        return result;
    }


}
